package org.social.db.dao;

import com.sojoys.artifact.manager.DataSourceManager;
import java.sql.Connection;
import org.social.db.bean.Dictionaries;
import com.sojoys.artifact.build.data.base.BaseDao;
import java.util.ArrayList;
import com.sojoys.artifact.manager.JsonManager;
import java.sql.SQLException;
import com.sojoys.artifact.build.data.jdbc.SK_Query;
import com.google.common.collect.Lists;
import com.sojoys.artifact.tools.ToolMap;
import org.apache.commons.dbutils.handlers.MapHandler;
import com.sojoys.artifact.tools.SK_Plus;
import java.sql.ResultSet;
import java.util.Map;
import javax.sql.DataSource;
import org.apache.commons.dbutils.handlers.MapListHandler;
import com.sojoys.artifact.constant.CreateTableEnum;
import com.sojoys.artifact.constant.DataModelKeyEnum;
import org.apache.commons.dbutils.handlers.AbstractListHandler;
import org.apache.commons.dbutils.QueryRunner;
import com.xiaoleilu.hutool.log.Log;
import java.util.Set;
import com.sojoys.artifact.constant.ArtifactErrorCode;
import com.xiaoleilu.hutool.util.StrUtil;
import com.sojoys.artifact.tools.ToolError;
import org.apache.commons.dbutils.DbUtils;
import com.xiaoleilu.hutool.log.LogFactory;
import java.util.List;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.dbutils.ResultSetHandler;
import com.google.common.base.Joiner;
import static org.social.db.StaticResource.DictionariesResource.*;
import static org.social.db.StaticResource.DictionariesResource.*;
/**
 * 字典库
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public class DictionariesDaoImpl implements BaseDao<Integer,Dictionaries>{
	static Log log = LogFactory.get(DictionariesDaoImpl.class);
	
	// 代理
	
	/**
	 * 私有化无参构造方法
	 */
	private DictionariesDaoImpl() {}
	  
    private static final DictionariesDaoImpl INSTANCE = new DictionariesDaoImpl();  
    
    public static DictionariesDaoImpl me() {  
        return INSTANCE;  
    }  
	
	public Dictionaries insert(Dictionaries dictionaries){
		Connection conn = DataSourceManager.me().getMainConnection();
		return insert(dictionaries,conn);
	}
	
	public Dictionaries insert(Dictionaries dictionaries,Connection conn){
		return insert(dictionaries,conn,TABLE_NAME);
	}
	
	public Dictionaries insert(Dictionaries dictionaries,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return insert(dictionaries,conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public Dictionaries insert(Dictionaries dictionaries,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return insert(dictionaries,conn,tableName);
	}
	
	public Dictionaries insert(Dictionaries dictionaries,Connection conn,String tableName){
		
		SK_Query sq = new SK_Query();
		String sql = "INSERT INTO " +tableName+ " (id,language,content_key,content_val,create_date,modified_date) VALUES (?,?,?,?,?,?)";
		try {
			int i = (int)sq.insert(conn,sql,dictionaries.getId(),dictionaries.getLanguage(),dictionaries.getContentKey(),dictionaries.getContentVal(),dictionaries.getCreateDate(),dictionaries.getModifiedDate());
			if(dictionaries.getId()==0){
				dictionaries.setId(i);
			}
			return i > 0 ? dictionaries : null;
		} catch (Exception e) {
			log.error(e);
			return null;
		} finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
	}
	
	public Dictionaries insert(Dictionaries dictionaries,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return insert(dictionaries,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}		
	}
	
	public int[] insertBatch(List<Dictionaries> dictionariess){
		Connection conn = DataSourceManager.me().getMainConnection();
		return insertBatch(dictionariess,conn);
	}
	
	public int[] insertBatch(List<Dictionaries> dictionariess,Connection conn){
		return insertBatch(dictionariess,conn,TABLE_NAME);
	}
	
	public int[] insertBatch(List<Dictionaries> dictionariess,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return insertBatch(dictionariess,conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public int[] insertBatch(List<Dictionaries> dictionariess,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return insertBatch(dictionariess,conn,tableName);
	}
	
	public int[] insertBatch(List<Dictionaries> dictionariess,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "INSERT INTO " +tableName+ " (id,language,content_key,content_val,create_date,modified_date) VALUES (?,?,?,?,?,?)";
		try {
			int columnSize = 6;
			int size = dictionariess.size();
			Object[][] params = new Object[size][columnSize];
			for (int i = 0; i < size; i++) {
				params[i][0] =dictionariess.get(i).getId();
				params[i][1] =dictionariess.get(i).getLanguage();
				params[i][2] =dictionariess.get(i).getContentKey();
				params[i][3] =dictionariess.get(i).getContentVal();
				params[i][4] =dictionariess.get(i).getCreateDate();
				params[i][5] =dictionariess.get(i).getModifiedDate();
			}
			int[] is = run.batch(conn,sql,params);
			return is.length > 0 ? is : new int[]{};
		} catch (Exception e) {
			log.error(e);
			return new int[]{};
		} finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
	}
	
	public int[] insertBatch(List<Dictionaries> dictionariess,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return insertBatch(dictionariess,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}		
	}
	
	public Dictionaries update(Dictionaries dictionaries){
		Connection conn = DataSourceManager.me().getMainConnection();
		return update(dictionaries,conn);
	}
	
	public Dictionaries update(Dictionaries dictionaries,Connection conn){
		return update(dictionaries,conn,TABLE_NAME);
	}
	
	public Dictionaries update(Dictionaries dictionaries,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return update(dictionaries,conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public Dictionaries update(Dictionaries dictionaries,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return update(dictionaries,conn,tableName);
	}
	
	public Dictionaries update(Dictionaries dictionaries,Connection conn,String tableName){
		List<Dictionaries> dictionariess = new ArrayList<Dictionaries>();
		dictionariess.add(dictionaries);
		updateBatch(dictionariess,conn,tableName);
		return dictionaries;
	}
	
	public Dictionaries update(Dictionaries dictionaries,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return update(dictionaries,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public int[] updateBatch(List<Dictionaries> dictionariess){
		Connection conn = DataSourceManager.me().getMainConnection();
		return updateBatch(dictionariess,conn);
	}
	
	public int[] updateBatch(List<Dictionaries> dictionariess,Connection conn){
		return updateBatch(dictionariess,conn,TABLE_NAME);
	}
	
	public int[] updateBatch(List<Dictionaries> dictionariess,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return updateBatch(dictionariess,conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public int[] updateBatch(List<Dictionaries> dictionariess,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return updateBatch(dictionariess,conn,tableName);
	}
	
	public int[] updateBatch(List<Dictionaries> dictionariess,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "UPDATE " +tableName+ " SET id = ?,language = ?,content_key = ?,content_val = ?,create_date = ?,modified_date = ? WHERE id = ?";
		try {
			int columnSize = 6;
			int size = dictionariess.size();
			Object[][] params = new Object[size][columnSize + 1];
			for (int i = 0; i < size; i++) {
				params[i][0] =dictionariess.get(i).getId();
				params[i][1] =dictionariess.get(i).getLanguage();
				params[i][2] =dictionariess.get(i).getContentKey();
				params[i][3] =dictionariess.get(i).getContentVal();
				params[i][4] =dictionariess.get(i).getCreateDate();
				params[i][5] =dictionariess.get(i).getModifiedDate();
				params[i][columnSize] =dictionariess.get(i).getId();
			}
			int[] is = run.batch(conn,sql,params);
			return is.length > 0 ? is : new int[]{};
		} catch (Exception e) {
			log.error(e);
			return new int[]{};
		} finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
	}
	
	public int[] updateBatch(List<Dictionaries> dictionariess,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return updateBatch(dictionariess,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}		
	}
	
	public boolean delete(Dictionaries dictionaries){
		Connection conn = DataSourceManager.me().getMainConnection();
		return delete(dictionaries,conn);
	}
	
	public boolean delete(Dictionaries dictionaries,Connection conn){
		return delete(dictionaries,conn,TABLE_NAME);
	}
	
	public boolean delete(Dictionaries dictionaries,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return delete(dictionaries,conn);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	public boolean delete(Dictionaries dictionaries,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return delete(dictionaries,conn,tableName);
	}
	
	public boolean delete(Dictionaries dictionaries,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "DELETE FROM " + tableName + " WHERE id = ?";
		try {
			int i = run.update(conn,sql, dictionaries.getId());
			return i > 0 ? true : false;
		} catch (Exception e) {
			log.error(e);
			return false;
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return false;
			}
		}
	}
	
	public boolean delete(Dictionaries dictionaries,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return delete(dictionaries,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	
	public boolean deleteBatch(List<Dictionaries> dictionariess){
		Connection conn = DataSourceManager.me().getMainConnection();
		return deleteBatch(dictionariess,conn);
	}
	
	public boolean deleteBatch(List<Dictionaries> dictionariess,Connection conn){
		return deleteBatch(dictionariess,conn,TABLE_NAME);
	}
	
	public boolean deleteBatch(List<Dictionaries> dictionariess,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return deleteBatch(dictionariess,conn);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	public boolean deleteBatch(List<Dictionaries> dictionariess,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return deleteBatch(dictionariess,conn,tableName);
	}
	
	public boolean deleteBatch(List<Dictionaries> dictionariess,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "DELETE FROM " + tableName + " WHERE id = ?";
		try {
			int size = dictionariess.size();
			Object[][] params = new Object[size][1];
			for (int i = 0; i < size; i++) {
				params[i][0] = dictionariess.get(i).getId();
			}
			int[] is = run.batch(conn,sql,params);
			return is.length > 0;
		} catch (Exception e) {
			log.error(e);
			return false;
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return false;
			}
		}
	}
	
	public boolean deleteBatch(List<Dictionaries> dictionariess,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return deleteBatch(dictionariess,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}

	/**
	 * 根据( id ) 查询
	 */
	public Dictionaries getById(int id){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getById(id, conn);
	}
	
	public Dictionaries getById(int id,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getById(id, conn,tableName);
	}
	public List<Dictionaries> getIn(Set<Integer> ids){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getIn(ids, conn);
	}
	
	public List<Dictionaries> getIn(Set<Integer> ids,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getIn(ids, conn,tableName);
	}
	/**
	 * 根据( language  content_key ) 查询
	 */
	public Dictionaries getByLanguageContentKey(String language, String contentKey){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getByLanguageContentKey(language,contentKey, conn);
	}
	
	public Dictionaries getByLanguageContentKey(String language, String contentKey,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getByLanguageContentKey(language,contentKey, conn,tableName);
	}
	
	//Connection
	/**
	 * 根据( id ) 查询
	 */
	public Dictionaries getById(int id,Connection conn){
		return getById(id,conn,TABLE_NAME);
	}
	
	public Dictionaries getById(int id,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "SELECT id,language,content_key,content_val,create_date,modified_date FROM " + tableName + " WHERE " + "id = ? ORDER BY id ASC LIMIT 1";
		Dictionaries dictionaries = null; 
		try {
			dictionaries = run.query(conn,sql, new DictionariesHandler(), id);
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return dictionaries;
	}
	public List<Dictionaries> getIn(Set<Integer> ids,Connection conn){
		return getIn(ids,conn,TABLE_NAME);
	}
	
	public List<Dictionaries> getIn(Set<Integer> ids,Connection conn,String tableName){
		List<Dictionaries> dictionariess = Lists.newArrayList();
		if(ids.isEmpty()) return dictionariess;
		QueryRunner run = new QueryRunner();
		String sql = "SELECT id,language,content_key,content_val,create_date,modified_date FROM " + tableName + " WHERE " + "id in (%s) ORDER BY id ASC";
		sql = String.format(sql, Joiner.on(",").join(ids));
		try {
			dictionariess = run.query(conn,sql, new DictionariesListHandler());
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return dictionariess;
	}
	/**
	 * 根据( language  content_key ) 查询
	 */
	public Dictionaries getByLanguageContentKey(String language, String contentKey,Connection conn){
		return getByLanguageContentKey(language,contentKey,conn,TABLE_NAME);
	}
	
	public Dictionaries getByLanguageContentKey(String language, String contentKey,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "SELECT id,language,content_key,content_val,create_date,modified_date FROM " + tableName + " WHERE " + "language = ? AND content_key = ? ORDER BY id ASC LIMIT 1";
		Dictionaries dictionaries = null; 
		try {
			dictionaries = run.query(conn,sql, new DictionariesHandler(), language,contentKey);
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return dictionaries;
	}
	
	//DataSource
	/**
	 * 根据( id ) 查询
	 */
	public Dictionaries getById(int id,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return getById(id, conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public Dictionaries getById(int id,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return getById(id, conn, tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	public List<Dictionaries> getIn(Set<Integer> ids,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return getIn(ids, conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public List<Dictionaries> getIn(Set<Integer> ids,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return getIn(ids, conn, tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	/**
	 * 根据( language  content_key ) 查询
	 */
	public Dictionaries getByLanguageContentKey(String language, String contentKey,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return getByLanguageContentKey(language,contentKey, conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public Dictionaries getByLanguageContentKey(String language, String contentKey,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return getByLanguageContentKey(language,contentKey, conn, tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	
	public List<Dictionaries> getAll(){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getAll(conn);
	}
	
	public List<Dictionaries> getAll(Connection conn){
		return getAll(conn,TABLE_NAME);
	}
	
	public List<Dictionaries> getAll(DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return getAll(conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public List<Dictionaries> getAll(String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getAll(conn,tableName);
	}
	
	public List<Dictionaries> getAll(Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "SELECT id,language,content_key,content_val,create_date,modified_date FROM " + tableName + " ORDER BY id ASC";
		List<Dictionaries> dictionariess = Lists.newArrayList();
		try {
			dictionariess = run.query(conn, sql, new DictionariesListHandler());
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return dictionariess;
	}
	
	public List<Dictionaries> getAll(DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return getAll(conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}		
	}
	
	public List<Dictionaries> getAllPage(int page,int pageSize){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getAllPage(conn,page,pageSize);
	}
	
	public List<Dictionaries> getAllPage(Connection conn,int page,int pageSize){
		return getAllPage(conn,TABLE_NAME,page,pageSize);
	}
	
	public List<Dictionaries> getAllPage(DataSource ds,int page,int pageSize){
		try {
			Connection conn = ds.getConnection();
			return getAllPage(conn,page,pageSize);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public List<Dictionaries> getAllPage(String tableName,int page,int pageSize){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getAllPage(conn,tableName,page,pageSize);
	}
	
	public List<Dictionaries> getAllPage(Connection conn,String tableName,int page,int pageSize){
		QueryRunner run = new QueryRunner();
		page = ((page-1) * pageSize);
		String sql = "SELECT id,language,content_key,content_val,create_date,modified_date FROM " + tableName + " ORDER BY id ASC LIMIT " + page + " , " +pageSize;
		List<Dictionaries> dictionariess = Lists.newArrayList();
		try {
			dictionariess = run.query(conn, sql, new DictionariesListHandler());
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return dictionariess;
	}
	
	public List<Dictionaries> getAllPage(DataSource ds,String tableName,int page,int pageSize){
		try {
			Connection conn = ds.getConnection();
			return getAllPage(conn,tableName,page,pageSize);
		} catch (Exception e) {
			log.error(e);
			return null;
		}		
	}
	
	public boolean truncate(){
		Connection conn = DataSourceManager.me().getMainConnection();
		return truncate(conn);
	}
	
	public boolean truncate(Connection conn){
		return truncate(conn,TABLE_NAME);
	}
	
	public boolean truncate(DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return truncate(conn);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	public boolean truncate(String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return truncate(conn,tableName);
	}
	
	public boolean truncate(Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "TRUNCATE " + tableName;
		try {
			run.update(conn, sql);
			return true;
		} catch (Exception e) {
			log.error(e);
			return false;
		} finally {
			try {
				DbUtils.close(conn);
			} catch (Exception e1) {
				log.error(e1);
				return false;
			}
		}
	}
	
	public boolean truncate(DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return truncate(conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	// 执行自定义查询sql
	public List<Map<String,Object>> executeQuerySql(String sql){
		Connection conn = DataSourceManager.me().getMainConnection();
		return executeQuerySql(conn,sql);
	}
	
	public List<Map<String,Object>> executeQuerySql(Connection conn,String sql){
		return executeQuerySql(conn,TABLE_NAME,sql);
	}
	
	public List<Map<String,Object>> executeQuerySql(DataSource ds,String sql){
		try {
			Connection conn = ds.getConnection();
			return executeQuerySql(conn,sql);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public List<Map<String,Object>> executeQuerySql(String tableName,String sql){
		Connection conn = DataSourceManager.me().getMainConnection();
		return executeQuerySql(conn,tableName);
	}
	
	public List<Map<String,Object>> executeQuerySql(Connection conn,String tableName,String sql){
		QueryRunner run = new QueryRunner();
		ToolError.isAndTrue(ArtifactErrorCode.SQL_PERMISSIONS_ERROR, !StrUtil.startWithIgnoreCase(sql, "SELECT"));
		List<Map<String,Object>> list = Lists.newArrayList();
		try {
			list = run.query(conn,sql, new MapListHandler());
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return list;
	}
	/**
	public List<Map<String,Object>> executeQuerySql(DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return executeQuerySql(conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	*/
	
	//
	
	public int getLastId(){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getLastId(conn);
	}
	
	public int getLastId(Connection conn){
		return getLastId(conn,TABLE_NAME);
	}
	
	public int getLastId(DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return getLastId(conn);
		} catch (Exception e) {
			log.error(e);
			return 0;
		}
	}
	
	public int getLastId(String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getLastId(conn,tableName);
	}
	
	public int getLastId(Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "SELECT id FROM " + tableName + " ORDER BY id DESC LIMIT 1";
		try {
			Map result = run.query(conn, sql, new MapHandler());
			return result == null ? 0 : ToolMap.getInt("id",result,0);
		} catch (Exception e) {
			log.error(e);
			return 0;
		} finally {
			try {
				DbUtils.close(conn);
			} catch (Exception e1) {
				log.error(e1);
				return 0;
			}
		}
	}
	
	public int getLastId(DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return getLastId(conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return 0;
		}
	}
	//
	
	//Drop Table
	public boolean drop(){
		Connection conn = DataSourceManager.me().getMainConnection();
		return drop(conn);
	}
	
	public boolean drop(Connection conn){
		return drop(conn,TABLE_NAME);
	}
	
	public boolean drop(DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return drop(conn);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	public boolean drop(String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return drop(conn,tableName);
	}
	
	public boolean drop(Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "DROP TABLE " + tableName;
		try {
			run.update(conn, sql);
			return true;
		} catch (Exception e) {
			log.error(e);
			return false;
		} finally {
			try {
				DbUtils.close(conn);
			} catch (Exception e1) {
				log.error(e1);
				return false;
			}
		}
	}
	
	public boolean drop(DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return drop(conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	
	//create
	public boolean createTable(CreateTableEnum type){
		Connection conn = DataSourceManager.me().getMainConnection();
		return createTable(conn,type);
	}
	
	public boolean createTable(Connection conn,CreateTableEnum type){
		return createTable(conn,TABLE_NAME,type);
	}
	
	public boolean createTable(DataSource ds,CreateTableEnum type){
		try {
			Connection conn = ds.getConnection();
			return createTable(conn,type);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	public boolean createTable(String tableName,CreateTableEnum type){
		Connection conn = DataSourceManager.me().getMainConnection();
		return createTable(conn,tableName,type);
	}
	
	public boolean createTable(Connection conn,String tableName,CreateTableEnum type){
		QueryRunner run = new QueryRunner();
		SK_Plus plus = SK_Plus.b("CREATE TABLE IF NOT EXISTS `", tableName,"` (");
		plus.a("  `id` INT(11) NOT NULL AUTO_INCREMENT COMMENT '',");	
		plus.a("  `language` VARCHAR(45)   COMMENT 'remarks=语言类型',");	
		plus.a("  `content_key` VARCHAR(45)   COMMENT 'remarks=文字标识',");	
		plus.a("  `content_val` TEXT(21845)   COMMENT 'remarks=文字内容',");	
		plus.a("  `create_date` DATETIME   COMMENT '',");
		plus.a("  `modified_date` DATETIME   COMMENT '',");
		plus.a("  PRIMARY KEY (`id`),");
		if (CreateTableEnum.LOGIC.equals(type) || CreateTableEnum.DESIGN.equals(type)) {
			plus.a("  UNIQUE KEY `language_key` (`language`,`content_key`),");
			
		}
		if (CreateTableEnum.DESIGN.equals(type)) {
		}
		String sql = plus.e();
		sql = StrUtil.subPre(sql, sql.lastIndexOf(StrUtil.C_COMMA));
		sql+=") ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 COMMENT='remarks=字典库';";
		try {
			run.update(conn, sql);
			return true;
		} catch (Exception e) {
			log.error(e);
			return false;
		} finally {
			try {
				DbUtils.close(conn);
			} catch (Exception e1) {
				log.error(e1);
				return false;
			}
		}
	}
	
	public boolean createTable(DataSource ds,String tableName,CreateTableEnum type){
		try {
			Connection conn = ds.getConnection();
			return createTable(conn,tableName,type);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	
	public String getTableName(){
		return TABLE_NAME;
	}
	
	@SuppressWarnings("unchecked")
	public List<Map<String,Object>> getColumnsInfo(){
		SK_Plus sp = SK_Plus.newSK_Plus();
		sp.a("[");
       	sp.a("{\"SCOPE_TABLE\":null,\"TABLE_CAT\":\"dal_social_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"NO\",\"TABLE_NAME\":\"dictionaries\",\"COLUMN_DEF\":null,\"SCOPE_CATALOG\":null,\"TABLE_SCHEM\":null,\"COLUMN_NAME\":\"id\",\"NULLABLE\":0,\"REMARKS\":\"\",\"DECIMAL_DIGITS\":0,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"YES\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":null,\"ORDINAL_POSITION\":1,\"SCOPE_SCHEMA\":null,\"SOURCE_DATA_TYPE\":null,\"DATA_TYPE\":4,\"TYPE_NAME\":\"INT\",\"COLUMN_SIZE\":11}").a(",");
       	sp.a("{\"SCOPE_TABLE\":null,\"TABLE_CAT\":\"dal_social_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"dictionaries\",\"COLUMN_DEF\":null,\"SCOPE_CATALOG\":null,\"TABLE_SCHEM\":null,\"COLUMN_NAME\":\"language\",\"NULLABLE\":1,\"REMARKS\":\"remarks=语言类型\",\"DECIMAL_DIGITS\":null,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":135,\"ORDINAL_POSITION\":2,\"SCOPE_SCHEMA\":null,\"SOURCE_DATA_TYPE\":null,\"DATA_TYPE\":12,\"TYPE_NAME\":\"VARCHAR\",\"COLUMN_SIZE\":45}").a(",");
       	sp.a("{\"SCOPE_TABLE\":null,\"TABLE_CAT\":\"dal_social_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"dictionaries\",\"COLUMN_DEF\":null,\"SCOPE_CATALOG\":null,\"TABLE_SCHEM\":null,\"COLUMN_NAME\":\"content_key\",\"NULLABLE\":1,\"REMARKS\":\"remarks=文字标识\",\"DECIMAL_DIGITS\":null,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":135,\"ORDINAL_POSITION\":3,\"SCOPE_SCHEMA\":null,\"SOURCE_DATA_TYPE\":null,\"DATA_TYPE\":12,\"TYPE_NAME\":\"VARCHAR\",\"COLUMN_SIZE\":45}").a(",");
       	sp.a("{\"SCOPE_TABLE\":null,\"TABLE_CAT\":\"dal_social_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"dictionaries\",\"COLUMN_DEF\":null,\"SCOPE_CATALOG\":null,\"TABLE_SCHEM\":null,\"COLUMN_NAME\":\"content_val\",\"NULLABLE\":1,\"REMARKS\":\"remarks=文字内容\",\"DECIMAL_DIGITS\":null,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":65535,\"ORDINAL_POSITION\":4,\"SCOPE_SCHEMA\":null,\"SOURCE_DATA_TYPE\":null,\"DATA_TYPE\":-1,\"TYPE_NAME\":\"TEXT\",\"COLUMN_SIZE\":21845}").a(",");
       	sp.a("{\"SCOPE_TABLE\":null,\"TABLE_CAT\":\"dal_social_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"dictionaries\",\"COLUMN_DEF\":null,\"SCOPE_CATALOG\":null,\"TABLE_SCHEM\":null,\"COLUMN_NAME\":\"create_date\",\"NULLABLE\":1,\"REMARKS\":\"\",\"DECIMAL_DIGITS\":null,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":null,\"ORDINAL_POSITION\":5,\"SCOPE_SCHEMA\":null,\"SOURCE_DATA_TYPE\":null,\"DATA_TYPE\":93,\"TYPE_NAME\":\"DATETIME\",\"COLUMN_SIZE\":19}").a(",");
       	sp.a("{\"SCOPE_TABLE\":null,\"TABLE_CAT\":\"dal_social_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"dictionaries\",\"COLUMN_DEF\":null,\"SCOPE_CATALOG\":null,\"TABLE_SCHEM\":null,\"COLUMN_NAME\":\"modified_date\",\"NULLABLE\":1,\"REMARKS\":\"\",\"DECIMAL_DIGITS\":null,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":null,\"ORDINAL_POSITION\":6,\"SCOPE_SCHEMA\":null,\"SOURCE_DATA_TYPE\":null,\"DATA_TYPE\":93,\"TYPE_NAME\":\"DATETIME\",\"COLUMN_SIZE\":19}").a(",");
        sp.a("]");
        return JsonManager.me().getJson().parse(sp.e(), List.class);
	}
	
	@SuppressWarnings("unchecked")
	public List<Map<String,Object>> getIndexsInfo(){
		SK_Plus sp = SK_Plus.newSK_Plus();
		sp.a("[");
       	sp.a("{\"NON_UNIQUE\":false,\"TABLE_CAT\":\"dal_social_design\",\"PAGES\":null,\"TABLE_NAME\":\"dictionaries\",\"TABLE_SCHEM\":null,\"COLUMN_NAME\":\"id\",\"INDEX_NAME\":\"PRIMARY\",\"ASC_OR_DESC\":\"A\",\"INDEX_QUALIFIER\":\"dal_social_design\",\"ORDINAL_POSITION\":1,\"CARDINALITY\":0,\"FILTER_CONDITION\":null,\"TYPE\":3}").a(",");
       	sp.a("{\"NON_UNIQUE\":false,\"TABLE_CAT\":\"dal_social_design\",\"PAGES\":null,\"TABLE_NAME\":\"dictionaries\",\"TABLE_SCHEM\":null,\"COLUMN_NAME\":\"language\",\"INDEX_NAME\":\"language_key\",\"ASC_OR_DESC\":\"A\",\"INDEX_QUALIFIER\":\"dal_social_design\",\"ORDINAL_POSITION\":1,\"CARDINALITY\":0,\"FILTER_CONDITION\":null,\"TYPE\":3}").a(",");
       	sp.a("{\"NON_UNIQUE\":false,\"TABLE_CAT\":\"dal_social_design\",\"PAGES\":null,\"TABLE_NAME\":\"dictionaries\",\"TABLE_SCHEM\":null,\"COLUMN_NAME\":\"content_key\",\"INDEX_NAME\":\"language_key\",\"ASC_OR_DESC\":\"A\",\"INDEX_QUALIFIER\":\"dal_social_design\",\"ORDINAL_POSITION\":2,\"CARDINALITY\":0,\"FILTER_CONDITION\":null,\"TYPE\":3}").a(",");
        sp.a("]");
        return JsonManager.me().getJson().parse(sp.e(), List.class);
	}
//自定义内容起始位置
class DictionariesHandler implements ResultSetHandler<Dictionaries>{
	@Override
	public Dictionaries handle(ResultSet rs) throws SQLException {
		return rs.next() ? Dictionaries.builder().createForResultSet(rs).syncIndexValues() : null ;
	}
}

class DictionariesListHandler extends AbstractListHandler<Dictionaries>{
	@Override
	protected Dictionaries handleRow(ResultSet rs) throws SQLException {
		return Dictionaries.builder().createForResultSet(rs).syncIndexValues();
	}
}

//自定义内容结束位置
}

